package com.gw.controller;


import com.gw.context.BaseContext;
import com.gw.pojo.entity.Bucket;
import com.gw.pojo.vo.FileVO;
import com.gw.result.Result;
import com.gw.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 新建桶（管理员操作）
     * @param bucketName
     * @return
     */
    @PostMapping("/addBucket")
    public Result addBucket (String bucketName){
        //判断当前角色
        Long currentUserId = BaseContext.getCurrentUserId();
        if (currentUserId != 0){
            throw new RuntimeException("只有管理员可操作");
        }
        //判断桶名合法性
        if (bucketName==null || bucketName.isEmpty()){
            throw new IllegalArgumentException("桶名为空");
        }
        log.info("新建桶");
        fileService.addBucket(bucketName);
        return Result.success();
    }


    /**
     * 向指定桶上传文件（管理员操作）
     * @param bucketName file
     * @return
     */
    @PostMapping("/upload/{bucketName}")
    public Result uploadFile (@PathVariable String bucketName,
                              @RequestParam MultipartFile file){
        //判断当前角色
        Long currentUserId = BaseContext.getCurrentUserId();
        if (currentUserId != 0){
            throw new RuntimeException("只有管理员可操作");
        }
        log.info("向指定桶添加文件{}",bucketName);
        if (file==null || file.isEmpty()){
            throw new RuntimeException("文件为空！");
        }
        fileService.uploadFile(bucketName, file);
        return Result.success();
    }


    /**
     * 删除桶（管理员操作）
     * @param bucketName
     * @return
     */
    @DeleteMapping("/removeBucket/{bucketName}")
    public Result removeBucket (@PathVariable String bucketName){
        //判断当前角色
        Long currentUserId = BaseContext.getCurrentUserId();
        if (currentUserId != 0){
            throw new RuntimeException("只有管理员可操作");
        }
        log.info("移除桶{}",bucketName);
        if (bucketName==null || bucketName.isEmpty()){
            throw new RuntimeException("桶名为空");
        }
        fileService.removeBucket(bucketName);
        return Result.success();
    }

    /**
     * 查询桶列表
     * @return
     */
    @GetMapping("/getBuckets")
    public Result<List<Bucket>> getBuckets (){
        log.info("获取桶列表...");
        List<Bucket> list = fileService.getBuckets();
        return Result.success(list);
    }

    /**
     * 查询指定桶内所有文件
     * @param bucketName
     * @return
     */
    @GetMapping("/getFiles")
    public Result<List<FileVO>> getFiles(String bucketName){
        log.info("查询桶：{}内所有文件", bucketName);
        List<FileVO> list = fileService.getFiles(bucketName);
        return Result.success(list);
    }

    /**
     * 下载文件（为前端返回下载链接）
     * @param id
     * @return
     */
    @GetMapping("/downloadFile/{id}")
    public Result<String> downloadFile(@PathVariable Long id){
        log.info("下载id为{}的文件", id);
        String url = fileService.downloadFile(id);

        return Result.success(url);
    }
}
